package com.bum.api.core.exp.selector;

import org.springframework.remoting.support.RemoteInvocation;

import com.asao.baseware.core.socket.remoting.IServiceConnectorSelector;
import com.asao.baseware.core.socket.remoting.ServiceConnector;

public class ApiSelector implements IServiceConnectorSelector {
	private int	counter;

	@Override
	public ServiceConnector selectFirst(ServiceConnector connectors[], RemoteInvocation ri) {
		return connectors[Math.abs(++counter % connectors.length)];
	}

	@Override
	public ServiceConnector selectNext(ServiceConnector connectors[], RemoteInvocation ri, ServiceConnector lastConnector, Throwable reason) {
		ServiceConnector conn = connectors[Math.abs(++counter % connectors.length)];
		if (conn.getHost().equals(lastConnector.getHost())) {
			conn = connectors[Math.abs(++counter % connectors.length)];
		}
		return conn;
	}
}
